package com.onehash.onebilling.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.BorderFactory;
import java.awt.Color;
import java.awt.BorderLayout;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.awt.Font;




import com.onehash.onebilling.entity.*;
import java.awt.Dimension;

public class OneBillingMainFrame extends JFrame {
    JPanel contentPane;
    BorderLayout mainBorderLayout = new BorderLayout();
    JMenuBar JMBmainMenu = new JMenuBar();
    JMenu JMfile = new JMenu();
    JMenuItem JMIfileExit = new JMenuItem();
    JMenu JMhelp = new JMenu();
    JMenuItem JMIhelpAbout = new JMenuItem();
    JToolBar JTBmainToolbar = new JToolBar();
    JLabel statusBar = new JLabel();
    JMenuItem JMIfileNew = new JMenuItem();
    JButton TBnew = new JButton();
    JButton TBchangePassword = new JButton();
    JMenuItem JMIchangePassword = new JMenuItem();
    
    User loginUser;
    
    public OneBillingMainFrame() {
        try {
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    /**
     * Component initialization.
     *
     * @throws java.lang.Exception
     */
    private void jbInit() throws Exception {
        setIconImage(Toolkit.getDefaultToolkit().createImage(OneBillingMainFrame.class.getResource("images/vrentIcon.png")));
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(mainBorderLayout);
        contentPane.setBackground(Color.gray);
        setSize(new Dimension(800, 570));
        setTitle(VersionControl.getProductName());
        this.addWindowListener(new OneBillingMainFrame_this_windowAdapter(this));
        statusBar.setBackground(Color.gray);
        statusBar.setFont(new java.awt.Font("Dialog", Font.BOLD, 12));
        statusBar.setForeground(Color.orange);
        statusBar.setBorder(BorderFactory.createLoweredBevelBorder());
        statusBar.setOpaque(true);
        statusBar.setText(" ");
        JMfile.setMnemonic('F');
        JMfile.setText("File");
        JMIfileExit.setMnemonic('X');
        JMIfileExit.setText("Exit");
        JMIfileExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.
                event.KeyEvent.VK_F4, java.awt.event.KeyEvent.ALT_MASK, false));
        JMIfileExit.addMouseListener(new
        		OneBillingMainFrame_JMIfileExit_mouseAdapter(this));
        JMIfileExit.addActionListener(new
        		OneBillingMainFrame_jMenuFileExit_ActionAdapter(this));
        JMhelp.setMnemonic('H');
        JMhelp.setText("Help");
        JMIhelpAbout.setMnemonic('A');
        JMIhelpAbout.setText("About");
        JMIhelpAbout.addMouseListener(new
        		OneBillingMainFrame_JMIhelpAbout_mouseAdapter(this));
        JMIhelpAbout.addActionListener(new
        		OneBillingMainFrame_jMenuHelpAbout_ActionAdapter(this));
        JMIfileNew.setText("New");
        JMIfileNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.
                event.KeyEvent.VK_N, java.awt.event.KeyEvent.CTRL_MASK, false));
        JMIfileNew.addMouseListener(new OneBillingMainFrame_JMIfileNew_mouseAdapter(this));
        JMIfileNew.addActionListener(new
        		OneBillingMainFrame_JMIfileNew_actionAdapter(this));
        TBnew.setBackground(Color.gray);
        TBnew.setIcon(new ImageIcon(OneBillingMainFrame.class.getResource("images/newTranIcon.png")));
        TBnew.addMouseListener(new OneBillingMainFrame_TBnew_mouseAdapter(this));
        TBnew.addActionListener(new OneBillingMainFrame_TBnew_actionAdapter(this));
        TBchangePassword.setBackground(Color.gray);
        TBchangePassword.setIcon(new ImageIcon(OneBillingMainFrame.class.getResource("images/chPasswordIcon.png")));
        TBchangePassword.addMouseListener(new
        		OneBillingMainFrame_TBchangePassword_mouseAdapter(this));
        TBchangePassword.addActionListener(new
        		OneBillingMainFrame_TBchangePassword_actionAdapter(this));
        JMIchangePassword.setText("Change Password");
        JMIchangePassword.addMouseListener(new
        		OneBillingMainFrame_JMIchangePassword_mouseAdapter(this));
        JMIchangePassword.addActionListener(new
        		OneBillingMainFrame_JMIchangePassword_actionAdapter(this));
        JTBmainToolbar.setBackground(Color.gray);
        JTBmainToolbar.setBorder(BorderFactory.createEtchedBorder());
        JTBmainToolbar.setFloatable(false);
        JMBmainMenu.add(JMfile);
        JMfile.add(JMIfileNew);
        JMfile.add(JMIchangePassword);
        JMfile.addSeparator();
        JMfile.add(JMIfileExit);
        JMBmainMenu.add(JMhelp);
        JMhelp.add(JMIhelpAbout);
        setJMenuBar(JMBmainMenu);
        contentPane.add(statusBar, BorderLayout.SOUTH);
        JTBmainToolbar.add(TBnew);
        JTBmainToolbar.add(TBchangePassword);
        contentPane.add(JTBmainToolbar, java.awt.BorderLayout.NORTH);
    }

    /* set status bar text */
    public void setStatusText(String text){
        this.statusBar.setText(text);
    }
    
    public void setLoginUser (User loginUser){
    	this.loginUser = loginUser;
    }

    /**
     * File | Exit action performed.
     *
     * @param actionEvent ActionEvent
     */
    void jMenuFileExit_actionPerformed(ActionEvent actionEvent) {
        MasterRecord masterRecord = MasterRecord.getInstance();
        
        try {
            masterRecord.serializeAll();
        } catch (FileNotFoundException fe) {

        } catch (IOException ie) {
            MessageDialog.showErrorDialog(this, this.getTitle(),
                                          "An I/O Error has occurred. Please contact System Administrator.");
            System.exit(1);
        }
        
        System.exit(0);
    }

    /**
     * Help | About action performed.
     *
     * @param actionEvent ActionEvent
     */
    void jMenuHelpAbout_actionPerformed(ActionEvent actionEvent) {
        OneBillingMainFrame_AboutBox dlg = new OneBillingMainFrame_AboutBox(this);
        Dimension dlgSize = dlg.getPreferredSize();
        Dimension frmSize = getSize();
        Point loc = getLocation();
        dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x,
                        (frmSize.height - dlgSize.height) / 2 + loc.y);
        dlg.setModal(true);
        dlg.pack();
        this.setStatusText("Ready.");
        dlg.setVisible(true);
    }

    public void JMIfileNew_actionPerformed(ActionEvent e) {
        this.setStatusText("Ready.");
        OneBillingNewTransactionDlg newTrasactDlg=new OneBillingNewTransactionDlg(this, this.getTitle()+loginUser.getUserName()+" - New Transaction", true, loginUser);
        newTrasactDlg.setVisible(true);
    }

    public void TBnew_actionPerformed(ActionEvent e) {
    	OneBillingNewTransactionDlg newTrasactDlg=new OneBillingNewTransactionDlg(this, this.getTitle()+loginUser.getUserName()+" - New Transaction", true,loginUser);
        newTrasactDlg.setVisible(true);
    }

    public void TBchangePassword_actionPerformed(ActionEvent e) {
    	OneBillingChPassDlg chPassDlg=new OneBillingChPassDlg((Frame)this.getOwner(), this.getTitle()+" - Change Password", true);
        try {
            chPassDlg.setControl(UserRegister.getInstance());
        } catch (FileNotFoundException fe) {
            MessageDialog.showErrorDialog(this, this.getTitle(),
                    "Login Information File was not found. Please contact System Administrator.");
            System.exit(1);
        } catch (IOException ie) {
            MessageDialog.showErrorDialog(this, this.getTitle(),
                    "An I/O Error has occurred. Please contact System Administrator.");
            System.exit(1);
        }
        chPassDlg.setVisible(true);
    }

    public void JMIchangePassword_actionPerformed(ActionEvent e) {
        this.setStatusText("Ready.");
        OneBillingChPassDlg chPassDlg=new OneBillingChPassDlg((Frame)this.getOwner(), this.getTitle()+" - Change Password", true);
        try {
            chPassDlg.setControl(UserRegister.getInstance());
        } catch (FileNotFoundException fe) {
            MessageDialog.showErrorDialog(this, this.getTitle(),
                    "Login Information File was not found. Please contact System Administrator.");
            System.exit(1);
        } catch (IOException ie) {
            MessageDialog.showErrorDialog(this, this.getTitle(),
                    "An I/O Error has occurred. Please contact System Administrator.");
            System.exit(1);
        }
        chPassDlg.setVisible(true);
    }

    public void center(){
        // Center the main frame window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = this.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        this.setLocation((screenSize.width - frameSize.width) / 2,
                          ((screenSize.height - frameSize.height) / 2)-15);

        /***********************************************/
    }

    public void TBnew_mouseEntered(MouseEvent e) {
        this.setStatusText("New Transaction.");
    }

    public void TBnew_mouseExited(MouseEvent e) {
        this.setStatusText("Ready.");
    }

    public void TBchangePassword_mouseEntered(MouseEvent e) {
        this.setStatusText("Change Login Password.");
    }

    public void TBchangePassword_mouseExited(MouseEvent e) {
        this.setStatusText("Ready.");
    }

    public void JMIfileNew_mouseEntered(MouseEvent e) {
        this.setStatusText("New Transaction.");
    }

    public void JMIfileNew_mouseExited(MouseEvent e) {
        this.setStatusText("Ready.");
    }

    public void JMIchangePassword_mouseEntered(MouseEvent e) {
        this.setStatusText("Change Login Password.");
    }

    public void JMIchangePassword_mouseExited(MouseEvent e) {
        this.setStatusText("Ready.");
    }

    public void JMIfileExit_mouseEntered(MouseEvent e) {
        this.setStatusText("Exits the System.");
    }

    public void JMIfileExit_mouseExited(MouseEvent e) {
        this.setStatusText("Ready.");
    }

    public void JMIhelpAbout_mouseEntered(MouseEvent e) {
        this.setStatusText("About One Billing System.");
    }

    public void JMIhelpAbout_mouseExited(MouseEvent e) {
        this.setStatusText("Ready.");
    }

    public void this_windowClosing(WindowEvent e) {
        MasterRecord masterRecord = MasterRecord.getInstance();
        try {
            masterRecord.serializeAll();
        } catch (FileNotFoundException fe) {

        } catch (IOException ie) {
            MessageDialog.showErrorDialog(this, this.getTitle(),
                                          "An I/O Error has occurred. Please contact System Administrator.");
            System.exit(1);
        }
    }
}


class OneBillingMainFrame_this_windowAdapter extends WindowAdapter {
    private OneBillingMainFrame adaptee;
    OneBillingMainFrame_this_windowAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void windowClosing(WindowEvent e) {
        adaptee.this_windowClosing(e);
    }
}


class OneBillingMainFrame_JMIchangePassword_actionAdapter implements ActionListener {
    private OneBillingMainFrame adaptee;
    OneBillingMainFrame_JMIchangePassword_actionAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.JMIchangePassword_actionPerformed(e);
    }
}


class OneBillingMainFrame_TBnew_actionAdapter implements ActionListener {
    private OneBillingMainFrame adaptee;
    OneBillingMainFrame_TBnew_actionAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.TBnew_actionPerformed(e);
    }
}


class OneBillingMainFrame_TBnew_mouseAdapter extends MouseAdapter {
    private OneBillingMainFrame adaptee;
    OneBillingMainFrame_TBnew_mouseAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void mouseEntered(MouseEvent e) {
        adaptee.TBnew_mouseEntered(e);
    }

    public void mouseExited(MouseEvent e) {
        adaptee.TBnew_mouseExited(e);
    }
}


class OneBillingMainFrame_TBchangePassword_actionAdapter implements ActionListener {
    private OneBillingMainFrame adaptee;
    OneBillingMainFrame_TBchangePassword_actionAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.TBchangePassword_actionPerformed(e);
    }
}


class OneBillingMainFrame_TBchangePassword_mouseAdapter extends MouseAdapter {
    private OneBillingMainFrame adaptee;
    OneBillingMainFrame_TBchangePassword_mouseAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void mouseEntered(MouseEvent e) {
        adaptee.TBchangePassword_mouseEntered(e);
    }

    public void mouseExited(MouseEvent e) {
        adaptee.TBchangePassword_mouseExited(e);
    }
}


class OneBillingMainFrame_JMIfileNew_actionAdapter implements ActionListener {
    private OneBillingMainFrame adaptee;
    OneBillingMainFrame_JMIfileNew_actionAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent e) {
        adaptee.JMIfileNew_actionPerformed(e);
    }
}


class OneBillingMainFrame_JMIfileNew_mouseAdapter extends MouseAdapter {
    private OneBillingMainFrame adaptee;
    OneBillingMainFrame_JMIfileNew_mouseAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void mouseEntered(MouseEvent e) {
        adaptee.JMIfileNew_mouseEntered(e);
    }

    public void mouseExited(MouseEvent e) {
        adaptee.JMIfileNew_mouseExited(e);
    }
}


class OneBillingMainFrame_JMIchangePassword_mouseAdapter extends MouseAdapter {
    private OneBillingMainFrame adaptee;
    OneBillingMainFrame_JMIchangePassword_mouseAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void mouseEntered(MouseEvent e) {
        adaptee.JMIchangePassword_mouseEntered(e);
    }

    public void mouseExited(MouseEvent e) {
        adaptee.JMIchangePassword_mouseExited(e);
    }
}


class OneBillingMainFrame_jMenuFileExit_ActionAdapter implements ActionListener {
	OneBillingMainFrame adaptee;

	OneBillingMainFrame_jMenuFileExit_ActionAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent actionEvent) {
        adaptee.jMenuFileExit_actionPerformed(actionEvent);
    }
}


class OneBillingMainFrame_JMIfileExit_mouseAdapter extends MouseAdapter {
    private OneBillingMainFrame adaptee;
    OneBillingMainFrame_JMIfileExit_mouseAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void mouseEntered(MouseEvent e) {
        adaptee.JMIfileExit_mouseEntered(e);
    }

    public void mouseExited(MouseEvent e) {
        adaptee.JMIfileExit_mouseExited(e);
    }
}


class OneBillingMainFrame_jMenuHelpAbout_ActionAdapter implements ActionListener {
	OneBillingMainFrame adaptee;

	OneBillingMainFrame_jMenuHelpAbout_ActionAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void actionPerformed(ActionEvent actionEvent) {
        adaptee.jMenuHelpAbout_actionPerformed(actionEvent);
    }
}


class OneBillingMainFrame_JMIhelpAbout_mouseAdapter extends MouseAdapter {
    private OneBillingMainFrame adaptee;
    OneBillingMainFrame_JMIhelpAbout_mouseAdapter(OneBillingMainFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void mouseEntered(MouseEvent e) {
        adaptee.JMIhelpAbout_mouseEntered(e);
    }

    public void mouseExited(MouseEvent e) {
        adaptee.JMIhelpAbout_mouseExited(e);
    }
}
